package com.agileboot.admin.controller.system;

import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.system.tag.TagApplicationService;
import com.agileboot.domain.system.tag.command.TagAddCommand;
import com.agileboot.domain.system.tag.command.TagUpdateCommand;
import com.agileboot.domain.system.tag.dto.TagDTO;
import com.agileboot.domain.system.tag.query.TagQuery;
import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.infrastructure.annotations.unrepeatable.Unrepeatable;
import com.agileboot.infrastructure.annotations.unrepeatable.Unrepeatable.CheckType;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 标签 信息操作处理
 */
@Tag(name = "标签API", description = "标签相关的增删查改")
@RestController
@RequestMapping("/system/tags")
@Validated
@RequiredArgsConstructor
public class SysTagController extends BaseController {

    private final TagApplicationService tagApplicationService;

    /**
     * 获取标签列表
     */
    @Operation(summary = "标签列表")
    // @PreAuthorize("@permission.has('system:tag:list')")
    @GetMapping
    public ResponseDTO<PageDTO<TagDTO>> list(TagQuery query) {
        PageDTO<TagDTO> pageDTO = tagApplicationService.getTagList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 从从库获取标签列表（示例）
     */
    @Operation(summary = "标签列表（从数据库从库获取）", description = "演示主从库的例子")
    @DS("slave")
    // @PreAuthorize("@permission.has('system:tag:list')")
    @GetMapping("/database/slave")
    public ResponseDTO<PageDTO<TagDTO>> listFromSlave(TagQuery query) {
        PageDTO<TagDTO> pageDTO = tagApplicationService.getTagList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 根据标签ID获取详情
     */
    @Operation(summary = "标签详情")
    // @PreAuthorize("@permission.has('system:tag:query')")
    @GetMapping("/{tagId}")
    public ResponseDTO<TagDTO> getInfo(@PathVariable @NotNull @Positive Long tagId) {
        return ResponseDTO.ok(tagApplicationService.getTagInfo(tagId));
    }

    /**
     * 新增标签
     */
    @Operation(summary = "添加标签")
    @Unrepeatable(interval = 60, checkType = CheckType.SYSTEM_USER)
    // @PreAuthorize("@permission.has('system:tag:add')")
    @AccessLog(title = "系统标签", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody TagAddCommand addCommand) {
        tagApplicationService.addTag(addCommand);
        return ResponseDTO.ok();
    }

    /**
     * 修改标签
     */
    @Operation(summary = "修改标签")
    // @PreAuthorize("@permission.has('system:tag:edit')")
    @AccessLog(title = "系统标签", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{tagId}")
    public ResponseDTO<Void> edit(@PathVariable Long tagId, @RequestBody TagUpdateCommand updateCommand) {
        updateCommand.setTagId(tagId);
        tagApplicationService.updateTag(updateCommand);
        return ResponseDTO.ok();
    }

    /**
     * 删除标签
     */
    @Operation(summary = "删除标签")
    // @PreAuthorize("@permission.has('system:tag:remove')")
    @AccessLog(title = "系统标签", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam List<Integer> tagIds) {
        tagApplicationService.deleteTag(new BulkOperationCommand<>(tagIds));
        return ResponseDTO.ok();
    }
}
